package com.aiyige.page.publish;


import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.utils.ExcludeEmojiInputFilter;
import com.aiyige.utils.ExcludeSpaceInputFilter;
import com.aiyige.utils.ExcludeEnterInputFilter;
import com.aiyige.utils.ExcludeSpaceInputFilter;
import com.aiyige.utils.InputNumFilter;
import com.aiyige.utils.ToastX;
import com.aiyige.utils.UIHelper;
import com.aiyige.utils.ValidateUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class CommonAddDialog extends DialogFragment {
    int titleResId;
    int hintResId;
    int maxInputNum;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.et)
    EditText et;
    @BindView(R.id.clearBtn)
    ImageView clearBtn;
    @BindView(R.id.inputNumTv)
    TextView inputNumTv;
    @BindView(R.id.maxInputNumTv)
    TextView maxInputNumTv;
    Unbinder unbinder;
    Listener listener;

    public CommonAddDialog() {
    }

    public static CommonAddDialog newInstance(int titleResId, int hintResId, int maxInputNum) {
        CommonAddDialog fragment = new CommonAddDialog();
        fragment.setStyle(STYLE_NORMAL, R.style.CommonDialogTheme);
        fragment.setTitleResId(titleResId);
        fragment.setHintResId(hintResId);
        fragment.setMaxInputNum(maxInputNum);
        return fragment;
    }

    public Listener getListener() {
        return listener;
    }

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public int getTitleResId() {
        return titleResId;
    }

    public void setTitleResId(int titleResId) {
        this.titleResId = titleResId;
    }

    public int getHintResId() {
        return hintResId;
    }

    public void setHintResId(int hintResId) {
        this.hintResId = hintResId;
    }

    public int getMaxInputNum() {
        return maxInputNum;
    }

    public void setMaxInputNum(int maxInputNum) {
        this.maxInputNum = maxInputNum;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_common_add, container, false);
        unbinder = ButterKnife.bind(this, view);
        titleTv.setText(titleResId);
        et.setHint(hintResId);
        InputNumFilter inputNumFilter = new InputNumFilter(maxInputNum, false);
        inputNumFilter.setListener(new InputNumFilter.Listener() {
            @Override
            public void inputNum(int num) {
                inputNumTv.setText(String.valueOf(num));
            }
        });
        et.setFilters(new InputFilter[]{new ExcludeSpaceInputFilter(), new ExcludeEmojiInputFilter(), inputNumFilter});
        inputNumTv.setText("0");
        maxInputNumTv.setText(String.valueOf(maxInputNum));
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() != 0) {
                    clearBtn.setVisibility(View.VISIBLE);
                } else {
                    clearBtn.setVisibility(View.INVISIBLE);
                }
            }
        });
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        Window window = getDialog().getWindow();
        if (window != null) {
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.width = Resources.getSystem().getDisplayMetrics().widthPixels - UIHelper.dip2px(50);
            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(layoutParams);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.cancelBtn, R.id.okBtn, R.id.clearBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cancelBtn:
                if (listener != null) {
                    listener.onCancel();
                }
                dismiss();
                break;
            case R.id.okBtn:
                if (listener != null) {
                    if (TextUtils.isEmpty(et.getText().toString().trim())) {
                        ToastX.show(R.string.please_input_tag_label);
                    } else {
                        listener.onOk(et.getText().toString().trim());
                        et.setText("");
                        dismiss();
                    }
                }
                break;
            case R.id.clearBtn:
                et.setText("");
                break;
        }
    }

    public interface Listener {
        void onOk(String str);

        void onCancel();
    }

}
